package com.squirrel.serialportcore.machine.init;

import com.squirrel.serialportcore.common.CommonBase;
import com.squirrel.serialportcore.machine.init.after.SerialPortInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 全局初始化管控(spring初始化后)
 *
 * @author Squbi
 * @date 2021/3/23 10:11
 */
@Component
public class GlobalInitializer implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(GlobalInitializer.class);

    private SerialPortInitializer serialPortInitializer;

    GlobalInitializer(SerialPortInitializer serialPortInitializer) {
        this.serialPortInitializer = serialPortInitializer;
    }

    /**
     * 最后优先级的初始化
     * 用于配置串口
     */
    @Override
    public void run(String... args) {
        for (String portName : CommonBase.getPortQueueName().keySet()) {
            serialPortInitializer.initSinglePort(portName);
        }
        logger.info(" ------- 初始化流程完成 -------");
        CommonBase.getStopWatch().stop();
        logger.info(" --- 初始化总计耗时 {} 毫秒 ---", CommonBase.getStopWatch().getTotalTimeMillis());
        CommonBase.setTotalInit(true);
    }
}
